﻿package com.youyun56.gps.android.common;

import java.lang.reflect.Method;

import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.util.Log;

public class NetWorkUtil {
    private static final int BLUETOOTH = 4;
    private static final int BRIGHTNESS = 1;
    private static final int GPS = 3;
    private static final int SYNC = 2;
    private static final int WIFI = 0;

    private static final String TAG = "NetWorkUtil";
    /**
     * 中国移动cmwap
     */
    public static final String CMWAP = "cmwap";

    /**
     * 中国移动cmnet
     */
    public static final String CMNET = "cmnet";

    // 中国联通3GWAP设置 中国联通3G因特网设置 中国联通WAP设置 中国联通因特网设置
    // 3gwap 3gnet uniwap uninet

    /**
     * 3G wap 中国联通3gwap APN
     */
    public static final String GWAP_3 = "3gwap";

    /**
     * 3G net 中国联通3gnet APN
     */
    public static final String GNET_3 = "3gnet";

    /**
     * uni wap 中国联通uni wap APN
     */
    public static final String UNIWAP = "uniwap";
    /**
     * uni net 中国联通uni net APN
     */
    public static final String UNINET = "uninet";

    /**
     * 中国电信 ctwap
     */
    public static final String CTWAP = "ctwap";

    /**
     * 中国电信 ctnet
     */
    public static final String CTNET = "ctnet";
    public static final String CTC = "CTC";

    /**
     * Private constructor to forbid instantiation.
     */
    private NetWorkUtil() {
    }

    /**
     * 是否有网络
     * 
     * @param context
     * @return
     */
    public static boolean getNetworkConnectionStatus(Context context) {
        try {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (manager == null) {
                Log.w(TAG, "getNetworkConnectionStatus,1,false");
                return false;
            }

            NetworkInfo info = manager.getActiveNetworkInfo();
            if (info == null) {
                Log.w(TAG, "getNetworkConnectionStatus,2,false");
                return false;
            }

            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (tm == null) {
                Log.w(TAG, "getNetworkConnectionStatus,3,false");
                return false;
            }

            if ((tm.getDataState() == TelephonyManager.DATA_CONNECTED || tm.getDataState() == TelephonyManager.DATA_ACTIVITY_NONE)
                    && info != null) {
                Log.w(TAG, "getNetworkConnectionStatus,true");
                return true;
            } else {
                Log.w(TAG, "getNetworkConnectionStatus,false");
                return false;
            }
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * 
     * @param context
     * @return 如果是中国联通uninet网络返回true，否则返回false
     */
    public static boolean isUNINet(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(UNINET) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    /**
     * 
     * @param context
     * @return 如果是中国联通uniwap网络返回true，否则返回false
     */
    public static boolean isUNIWap(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(UNIWAP) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    /**
     * 
     * @param context
     * @return 如果是中国移动cmwap网络返回true，否则返回false
     */
    public static boolean isCMWap(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(CMWAP) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    /**
     * 
     * @param context
     * @return 如果是中国联通3gwap网络返回true，否则返回false
     */
    public static boolean is3GWAP(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(GWAP_3) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    /**
     * 
     * @param context
     * @return 如果是中国联通3gnet网络返回true，否则返回false
     */
    public static boolean is3GNet(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(GNET_3) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    /**
     * 
     * @param context
     * @return 如果是中国联通cmnet网络返回true，否则返回false
     */
    public static boolean isCMNet(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(CMNET) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    public static boolean isCTNet(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null) {
                if (!extraInfo.equals(CTNET) ? true : false) {
                    return (extraInfo.equals(CTC) ? true : false);
                }
            } else {
                return false;
            }

        } else {
            return false;
        }
        return false;

    }

    public static boolean isCTWap(Context context) {

        ConnectivityManager conManager = (ConnectivityManager) context.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("MOBILE") != 0) {
                return false;
            }
            String extraInfo = ni.getExtraInfo();
            if (extraInfo != null)
                return (extraInfo.equals(CTWAP) ? true : false);
            else {
                return false;
            }

        } else {
            return false;
        }

    }

    /**
     * 是否WIFI网络
     * 
     * @param ctx
     * @return
     */
    public static boolean isWifi(final Context ctx) {
        ConnectivityManager conManager = (ConnectivityManager) ctx.

        getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo ni = conManager.getActiveNetworkInfo();

        if (ni != null) {
            if (ni.getTypeName().compareToIgnoreCase("WIFI") != 0) {
                return false;
            }
            return true;

        } else {
            return false;
        }
    }

    /**
     * 检测GPRS是否打开
     * 
     * @param context
     * @return
     */
    public static boolean isGprsOpen(Context context) {
        ConnectivityManager mCM;
        mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Class cmClass = mCM.getClass();
        Class[] argClasses = null;
        Object[] argObject = null;

        Boolean isOpen = false;
        try {
            Method method = cmClass.getMethod("getMobileDataEnabled", argClasses);

            isOpen = (Boolean) method.invoke(mCM, argObject);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isOpen;
    }

    /**
     * 开启/关闭GPRS
     * 
     * @param context
     * @param isEnable
     */
    public static void openGprs(Context context, boolean isEnable) {
        ConnectivityManager mCM;
        mCM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Class cmClass = mCM.getClass();
        Class[] argClasses = new Class[1];
        argClasses[0] = boolean.class;

        try {
            Method method = cmClass.getMethod("setMobileDataEnabled", argClasses);
            method.invoke(mCM, isEnable);
            if (isEnable) {
                Log.i(NetWorkUtil.class.getName(), "open gprs");
            } else {
                Log.i(NetWorkUtil.class.getName(), "close gprs");
            }
        } catch (Exception e) {
            Log.e(NetWorkUtil.class.getName(), e.getMessage(), e);
        }
    }

    /**
     * Checks if is umts.
     * 
     * @param ctx
     *            the ctx
     * @return true, if is umts
     */
    public static boolean isUmts(final Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getNetworkType() >= TelephonyManager.NETWORK_TYPE_UMTS;
    }

    /**
     * Checks if is edge.
     * 
     * @param ctx
     *            the ctx
     * @return true, if is edge
     */
    public static boolean isEdge(final Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE;
    }

    /**
     * 检查是否有网络(支持连接的网络)
     * 
     * @param void
     * 
     * @return 有网络true，否则false
     * 
     */
    public static boolean queryNetWork(final Context ctx) {

        // 判断当前网络是否支持连接服务器
        if (ctx == null) {
            throw new NullPointerException("Context == null");
        }

        try {
            boolean bConnection = NetWorkUtil.getNetworkConnectionStatus(ctx);
            Log.v(TAG, "NetworkConnectionStatus = " + bConnection);

            if (!bConnection) {
                return false;
            }
        } catch (Exception e) {
            return true;
        }
        // boolean bWifi = NetWorkUtil.isWifi(ctx);
        // Log.v(TAG, "Wifi = "+bWifi);
        // boolean bCMNet = NetWorkUtil.isCMNet(ctx);
        // Log.v(TAG, "CMNet = "+bCMNet);
        // boolean bUNINet = NetWorkUtil.isUNINet(ctx);
        // Log.v(TAG, "UNINet = "+bUNINet);
        // boolean b3GNet = NetWorkUtil.is3GNet(ctx);
        // Log.v(TAG, "3GNet = "+b3GNet);
        // boolean bCTNet = NetWorkUtil.isCTNet(ctx);
        // if (!bWifi && !bCMNet && !bUNINet && !b3GNet &&!bCTNet){
        // return false;
        // }

        return true;
    }

    public static boolean isSupportNetWork(final Context ctx) {

        try {
            boolean bCMWap = NetWorkUtil.isCMWap(ctx);
            boolean b3GWAP = NetWorkUtil.is3GWAP(ctx);
            boolean bCTWap = NetWorkUtil.isCTWap(ctx);
            boolean bUNIWap = NetWorkUtil.isUNIWap(ctx);
            if (b3GWAP || bCMWap || bCTWap || bUNIWap) {
                return false;
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    /**
     * 打开wifi 蓝牙，gps,sync
     * 
     * @param context
     * @param idx
     *            0wifi 1亮度 2sync 3gps 4蓝牙
     */
    public static void openNetWork(Context context, int idx) {
        Intent intent = new Intent();
        intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
        intent.addCategory("android.intent.category.ALTERNATIVE");
        Uri uri = Uri.parse("custom:" + idx);
        intent.setData(uri);
        try {
            PendingIntent.getBroadcast(context, 0, intent, 0).send();
        } catch (CanceledException e) {
            e.printStackTrace();
        }
    }
}
